﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_Header : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoginUser();
            view();
        }
    }
    public string TenDangNhap;
    public void LoginUser()
    {
        if (Request.Cookies["LoginUser"] != null)
        {
            InfoUser.Visible = true;
            linkDangNhap.Visible = false;
            lblNameUser.Text = Request.Cookies["LoginUser"]["HoTen"];
        }
        else
        { 
        if (Session["KhachHangID"]!=null)
        {
            InfoUser.Visible = true;
            linkDangNhap.Visible = false;
            lblNameUser.Text = Session["HoTen"].ToString();
        }
        }
    }
    public void view()
    {
        if (Session["Giohang"] != null)
        {
            DataTable dt = (DataTable)Session["Giohang"];

            if (dt.Rows.Count == 0)
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
            else
            {
                Panel1.Visible = false;
                Panel2.Visible = true;
                tinhtien(dt);
                gridGiohang.DataSource = dt;
                gridGiohang.DataBind();
            }
        }
        else
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
    }
    private void tinhtien(DataTable tb)
    {
        double tien = 0;
        foreach (DataRow dr in tb.Rows)
        {
            tien += double.Parse(dr[4].ToString());
        }
        lblTongtien.Text = tien.ToString();
    }
    protected void btnThoat_Click(object sender, EventArgs e)
    {
         if (Request.Cookies["LoginUser"] != null)
        {
            Response.Cookies["LoginUser"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect("Default.aspx");
        }
        else
        {

                Session.RemoveAll();
                Response.Redirect("Default.aspx");
        
        }


    }
}